package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.constant.enums.StockEnum;
import com.zdkj.subway.st.entity.SwStOutputDetail;
import com.zdkj.subway.st.entity.SwStTicketDestroy;
import com.zdkj.subway.st.entity.SwStTicketReceive;
import com.zdkj.subway.st.mapper.SwStTicketDestroyMapper;
import com.zdkj.subway.st.service.SwStTicketDestroyService;
import com.zdkj.subway.st.util.GenSerialNo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author kangjian
 * @version 1.0
 * @classname SwStTicketDestroyServiceImpl
 * @date 2020-01-06 22:39
 * @description
 */
@Service
@AllArgsConstructor
@Slf4j
public class SwStTicketDestroyServiceImpl extends ServiceImpl<SwStTicketDestroyMapper
        , SwStTicketDestroy> implements SwStTicketDestroyService {

    @Autowired
    private SwStOutputDetailServiceImpl sdcd;

    @Autowired
    private GenSerialNo genSerialNo;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SwStTicketDestroy swStTicketDestroy) {
        boolean issuccuess=true;
        String stocktype= StockEnum.StockType.OUTSTOCK.getValue();
        //销毁出库
        String outstockvalue=StockEnum.OutputStock.XHCK.getValue();
        try {
            //根据票种生成单号前缀
            String prefix = genSerialNo.getPreFixByTicketType(swStTicketDestroy.getDestroyCardType());
            String serialno=genSerialNo.genSerioNo(stocktype);
            PigxUser user = SecurityUtils.getUser();
            //执行出库操作
            SwStOutputDetail swStOutputDetail=SwStOutputDetail.builder().creatorId(user.getId()).creatorName(user.getUsername())
                    .ticketType(swStTicketDestroy.getDestroyCardType()).outGoingType(outstockvalue).createTime(LocalDateTime.now()).outstockno(prefix+serialno)
                    .stockArea(swStTicketDestroy.getStockArea()).ticketNumber(swStTicketDestroy.getDestroyNumber()).build();
            sdcd.save(swStOutputDetail);
            //其次更新入库基础表
            swStTicketDestroy.setCreatorId(user.getId());
            swStTicketDestroy.setCreatorName(user.getUsername());
            swStTicketDestroy.setCreate_time(LocalDateTime.now());
            //SwStTicketDestroy
            swStTicketDestroy.setOutputStockid(swStOutputDetail.getId());
            swStTicketDestroy.setOutputStockno(prefix+serialno);
            swStTicketDestroy.setOutStorageTime(swStOutputDetail.getOutGoingTime());
        }catch (Exception e){
            log.error("",e);
            throw new RuntimeException(e);
        }

        return issuccuess&&super.save(swStTicketDestroy);
    }

    /**
     * 通过库存更新领用信息
     * @param swStOutputDetail
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SwStOutputDetail swStOutputDetail) {
        try {
            SwStTicketDestroy swStTicketDestroy=new SwStTicketDestroy();
            swStTicketDestroy.setOutputStockno(swStOutputDetail.getOutstockno());
            swStTicketDestroy.setOutStorageTime(swStOutputDetail.getOutGoingTime());
            swStTicketDestroy.setDestroyCardType(swStOutputDetail.getTicketType());
            swStTicketDestroy.setActiveTime(swStOutputDetail.getActiveTime());
            swStTicketDestroy.setTicketNo(swStOutputDetail.getTicketNo());
            swStTicketDestroy.setDestroyNumber(swStOutputDetail.getTicketNumber());
            swStTicketDestroy.setStockArea(swStOutputDetail.getStockArea());
            swStTicketDestroy.setIsReceive("否");
            swStTicketDestroy.setOutputStockid(swStOutputDetail.getId());
            swStTicketDestroy.setTicketName(swStOutputDetail.getTicketName());
            swStTicketDestroy.setTicketPrice(swStOutputDetail.getTicketPrice());
            swStTicketDestroy.setTicketTimes(swStOutputDetail.getTicketTimes());
            swStTicketDestroy.setOutputStockid(swStOutputDetail.getId());
            swStTicketDestroy.setLatestStartTime(swStOutputDetail.getLatestStartTime());
            swStTicketDestroy.setFile(swStOutputDetail.getFilePath());
            swStTicketDestroy.setPicture(swStOutputDetail.getPicturePath());
            return super.save(swStTicketDestroy);
        }catch (Exception e){
            log.error("",e);
            throw new RuntimeException(e);
        }
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SwStTicketDestroy swStTicketDestroy){
        SwStOutputDetail swStOutputDetail=sdcd.getById(swStTicketDestroy.getOutputStockid());
        swStOutputDetail.setIsreceive("是");
        if(! sdcd.updateById(swStOutputDetail))
        {
            throw new RuntimeException("更新库存异常!");
        }
        //更新入库数据
        swStTicketDestroy.setIsReceive("是");
        return super.updateById(swStTicketDestroy);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        //级联删除库存
        SwStTicketDestroy SwStTicketDestroy= super.getById(id);
        long outputStockid=SwStTicketDestroy.getOutputStockid();
        if(! sdcd.removeById(outputStockid))
        {
            throw new RuntimeException("更新库存异常!");
        }
        return super.removeById(id);
    }
}